home *** CD-ROM | disk | FTP | other *** search
/ Delphi Magazine Collection 2001 / Delphi Magazine Collection 20001 (2001).iso / DISKS / Issue37 / Clinic / DBErrorU.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-05-14  |  1.9 KB  |  84 lines

  1. unit DBErrorU;
  2.  
  3. interface
  4.  
  5. uses
  6.   SysUtils, Windows, Messages, Classes, Graphics, Controls,
  7.   Forms, Dialogs, Grids, DBGrids, DB, DBTables, StdCtrls, ExtCtrls;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     DataSource1: TDataSource;
  12.     Table1: TTable;
  13.     DBGrid1: TDBGrid;
  14.     Panel1: TPanel;
  15.     Button1: TButton;
  16.     Button2: TButton;
  17.     procedure FormCreate(Sender: TObject);
  18.     procedure Button1Click(Sender: TObject);
  19.     procedure Button2Click(Sender: TObject);
  20.   public
  21.     procedure DoException(Sender: TObject; E: Exception);
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. uses
  30.   BDE, DBExcept;
  31.  
  32. {$R *.DFM}
  33.  
  34. procedure TForm1.DoException(Sender: TObject; E: Exception);
  35. begin
  36.   if E is EAccessViolation then
  37.     //Simple reaction to an Access Violation
  38.     ShowMessage('Ooch, Ouch, Ow!! That hurt!')
  39.   else
  40.     //Deal with all other exceptions in the normal way
  41.     Application.ShowException(E);
  42. end;
  43.  
  44. procedure TForm1.FormCreate(Sender: TObject);
  45. begin
  46.   //Set up personal default exception handler first
  47.   Application.OnException := DoException;
  48.   //Now create error dialog
  49.   DbEngineErrorDlg := TDbEngineErrorDlg.Create(Application);
  50.   //And install automatic support for it
  51.   DbEngineErrorDlg.HookExceptions
  52. end;
  53.  
  54. procedure TForm1.Button1Click(Sender: TObject);
  55. begin
  56.   //Generate an Access Violation
  57.   IntToStr(PInteger(nil)^)
  58. end;
  59.  
  60. procedure TForm1.Button2Click(Sender: TObject);
  61. begin
  62.   try
  63.     //Make sure we are not editing
  64.     Table1.Cancel;
  65.     //Record an existing unique key value
  66.     Tag := Table1['CustNo'];
  67.     //Add new record
  68.     Table1.Insert;
  69.     //Use same unique value
  70.     Table1['CustNo'] := Tag;
  71.     //Try and save, giving a key violation error
  72.     Table1.Post
  73.   except
  74.     on E: EDBEngineError do
  75.     begin
  76.       //Ask error form to deal with exception
  77.       DbEngineErrorDlg.ShowException(E);
  78.       Table1.Cancel;
  79.     end
  80.   end
  81. end;
  82.  
  83. end.
  84.